//head
#include <STC15F2K60S2.H>
#include <iic.h>
#include <Led.h>
#include <ds18B20.h>
#include <Seg.h>
#include <init.h>
#include <Key.h>
#include <ds1302.h>
//变量
unsigned char Key_Slow_Down,Key_Val,Key_Down,Key_Up,Key_Old;
unsigned char Seg_Slow_Down;
unsigned char Seg_Pos;
unsigned char Seg_Buf[8]={10,10,10,10,10,10,10,10};
unsigned char Seg_Point[8]={0,0,0,0,0,0,0,0};
unsigned char ucLed[8]={0,0,0,0,0,0,0,0};
unsigned char Seg_Mode;
unsigned char time_init[3]={23,59,50};
unsigned char Read_time[3]={11,23,23};
unsigned char log_time[3][3];//记录触发时间
unsigned char log_mode;//历史模式切换变量


float light_val,val;
float temp;//交换中间变量

//Key
void Key_Proc()
{
	if(Key_Slow_Down)return;
	Key_Slow_Down=1;
	
	Key_Val=Key_Read();
	Key_Down=Key_Val&(Key_Val^Key_Old);
	Key_Up=~Key_Val&(Key_Val^Key_Old);
	Key_Old=Key_Val;
	switch(Key_Down)
	{
		case 8:
			if(++Seg_Mode==3)
				Seg_Mode=0;
		break;
		case 9:
			if(Seg_Mode==2)
			{
				if(++log_mode==3)
					log_mode=0;
			}
		break;
		case 12:
			
		break;
	}
	
}

//Seg
void Seg_Proc()
{
	unsigned char i;
	if(Seg_Slow_Down)return;
	Seg_Slow_Down=1;
	/*信息获取区域*/
	Read_Rtc(Read_time);
	light_val=Ad_Read(0x41);
	val=Ad_Read(0x43);
	temp=light_val;
	light_val=val/51.0;
	val=temp/51.0;
	if(light_val>val)
		log_flag=0;
	else if(log_flag==0)
		log_flag=1;
	else
		log_flag=0;
	if(light_val<val)//数组推移继承
	{
		for(i=0;i<3;i++)
			log_time[2][i]=log_time[1][i];
		for(i=0;i<3;i++)
			log_time[1][i]=log_time[0][i];
		for(i=0;i<3;i++)
			log_time[0][i]=Read_time[i];
	}
	/*数据显示区域*/
	switch(Seg_Mode)
	{
		case 0:
			for(i=0;i<3;i++)
			{
				Seg_Buf[3*i]=Read_time[i]/10;
				Seg_Buf[3*i+1]=Read_time[i]%10;
			}
			Seg_Buf[2]=11;
			Seg_Buf[5]=11;
			Seg_Point[1]=0;//如何在其他界面熄灭高效？
			Seg_Point[5]=0;
		break;
		case 1:
			Seg_Buf[0]=13;
			Seg_Buf[1]=(unsigned char)light_val;
			Seg_Buf[2]=(unsigned char)(light_val*10)%10;
			Seg_Buf[3]=(unsigned int)(light_val*100)%10;
			Seg_Buf[4]=15;
			Seg_Buf[5]=(unsigned char)val;
			Seg_Buf[6]=(unsigned char)(val*10)%10;
			Seg_Buf[7]=(unsigned int)(val*100)%10;
			Seg_Point[1]=1;//如何在其他界面熄灭高效？
			Seg_Point[5]=1;
		break;
		case 2:
			Seg_Buf[0]=14;
			Seg_Buf[1]=log_mode+1;
			for(i=0;i<3;i++)
			{
				Seg_Buf[2+2*i]=log_time[log_mode][i]/10;
				Seg_Buf[3+2*i]=log_time[log_mode][i]%10;
			}
			Seg_Point[1]=0;//如何在其他界面熄灭高效？
			Seg_Point[5]=0;
		break;
		case 3:
			
		break;
		
	}
}

//Led 
void Led_Proc()
{
	
}

//T0
void Timer0_Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x80;			//定时器时钟1T模式
	TMOD &= 0xF0;			//设置定时器模式
	TL0 = 0x20;				//设置定时初始值
	TH0 = 0xD1;				//设置定时初始值
	TF0 = 0;				//清除TF0标志
	TR0 = 1;				//定时器0开始计时
	ET0=1;
	EA=1;
}

void Timer0_Server()interrupt 1
{
	if(++Key_Slow_Down==10)Key_Slow_Down=0;
	if(++Seg_Slow_Down==500)Seg_Slow_Down=0;
	if(++Seg_Pos==8)Seg_Pos=0;
	Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
	Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
}

void main()
{
	System_init();
	Set_Rtc(time_init);
	Timer0_Init();

	while(1)
	{
		
		Key_Proc();
		Seg_Proc();
		Led_Proc();
	}
}